package smap;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import BD.CursoBD;
import DAO.CursoDAO;

public class TelaCurso extends javax.swing.JDialog {

    CursoBD cur = new CursoBD();
    CursoDAO curDao = new CursoDAO();
    
    public TelaCurso(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupoCurso = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        btnProfessor = new javax.swing.JButton();
        btnUsuario = new javax.swing.JButton();
        btnCurso = new javax.swing.JButton();
        btnDisciplina = new javax.swing.JButton();
        btnAluno = new javax.swing.JButton();
        btnBackup = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        btnVoltar1 = new javax.swing.JButton();
        pnlControle = new javax.swing.JPanel();
        txtPesquisaCurso = new javax.swing.JTextField();
        cmbPesquisar = new javax.swing.JComboBox();
        btnPesquisar = new javax.swing.JButton();
        btnCarregartodos = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCurso = new javax.swing.JTable();
        btnExcluir = new javax.swing.JButton();
        btnCadastrar = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tela Curso");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));

        btnProfessor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/professor-icon.png"))); // NOI18N
        btnProfessor.setText("Professor");
        btnProfessor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProfessorActionPerformed(evt);
            }
        });

        btnUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/user-icon.png"))); // NOI18N
        btnUsuario.setText("Usuario");
        btnUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUsuarioActionPerformed(evt);
            }
        });

        btnCurso.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/curso-icon.png"))); // NOI18N
        btnCurso.setText("Curso");
        btnCurso.setEnabled(false);

        btnDisciplina.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/disciplina-icon.png"))); // NOI18N
        btnDisciplina.setText("Disciplina");
        btnDisciplina.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDisciplinaActionPerformed(evt);
            }
        });

        btnAluno.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/aluno-icon.png"))); // NOI18N
        btnAluno.setText("Aluno");
        btnAluno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlunoActionPerformed(evt);
            }
        });

        btnBackup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/backup-icon.png"))); // NOI18N
        btnBackup.setText("Backup");
        btnBackup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackupActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/turma-icon.png"))); // NOI18N
        jButton1.setText("Turma");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnAluno, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnBackup, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnProfessor)
                    .addComponent(btnUsuario)
                    .addComponent(btnAluno)
                    .addComponent(btnBackup)
                    .addComponent(btnDisciplina)
                    .addComponent(btnCurso, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));

        btnVoltar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/voltar-icon.png"))); // NOI18N
        btnVoltar1.setText("Voltar");
        btnVoltar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltar1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(517, 517, 517)
                .addComponent(btnVoltar1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnVoltar1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlControle.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)), "Pesquisar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.darkGray));

        cmbPesquisar.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nome do Curso", "Tipo do Curso", "Ano de Inicio" }));

        btnPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/procurar-icon.png"))); // NOI18N
        btnPesquisar.setText("Pesquisar");
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });

        btnCarregartodos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/todos-icon.png"))); // NOI18N
        btnCarregartodos.setText("Carregar todos");
        btnCarregartodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCarregartodosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlControleLayout = new javax.swing.GroupLayout(pnlControle);
        pnlControle.setLayout(pnlControleLayout);
        pnlControleLayout.setHorizontalGroup(
            pnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlControleLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtPesquisaCurso)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCarregartodos, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pnlControleLayout.setVerticalGroup(
            pnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlControleLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPesquisaCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPesquisar)
                    .addComponent(btnCarregartodos))
                .addGap(23, 23, 23))
        );

        tblCurso.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(192, 192, 192)));
        tblCurso.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nome", "Tipo do curso", "Ano criação", "Duração (anos)", "Créditos", "Tempo p/ concluir créditos"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblCurso.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblCurso.getTableHeader().setResizingAllowed(false);
        tblCurso.getTableHeader().setReorderingAllowed(false);
        tblCurso.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblCursoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblCurso);
        tblCurso.getColumnModel().getColumn(0).setMinWidth(80);
        tblCurso.getColumnModel().getColumn(0).setPreferredWidth(80);
        tblCurso.getColumnModel().getColumn(0).setMaxWidth(100);
        tblCurso.getColumnModel().getColumn(2).setMinWidth(120);
        tblCurso.getColumnModel().getColumn(2).setPreferredWidth(120);
        tblCurso.getColumnModel().getColumn(2).setMaxWidth(130);
        tblCurso.getColumnModel().getColumn(3).setMinWidth(100);
        tblCurso.getColumnModel().getColumn(3).setPreferredWidth(100);
        tblCurso.getColumnModel().getColumn(3).setMaxWidth(120);
        tblCurso.getColumnModel().getColumn(4).setMinWidth(120);
        tblCurso.getColumnModel().getColumn(4).setPreferredWidth(120);
        tblCurso.getColumnModel().getColumn(4).setMaxWidth(140);
        tblCurso.getColumnModel().getColumn(5).setMinWidth(100);
        tblCurso.getColumnModel().getColumn(5).setPreferredWidth(100);
        tblCurso.getColumnModel().getColumn(5).setMaxWidth(120);
        tblCurso.getColumnModel().getColumn(6).setMinWidth(180);
        tblCurso.getColumnModel().getColumn(6).setPreferredWidth(180);
        tblCurso.getColumnModel().getColumn(6).setMaxWidth(200);

        btnExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/delete-icon.png"))); // NOI18N
        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnCadastrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/add-icon.png"))); // NOI18N
        btnCadastrar.setText("Cadastrar");
        btnCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCadastrarActionPerformed(evt);
            }
        });

        btnAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/alterar-icon.png"))); // NOI18N
        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlControle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addGap(389, 389, 389)
                .addComponent(btnCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlControle, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExcluir)
                    .addComponent(btnCadastrar)
                    .addComponent(btnAlterar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void btnProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProfessorActionPerformed
        this.dispose(); // Fecha Tela Curso
        TelaProfessor pro = new TelaProfessor(null, true);
        pro.carregarTabela("");
        pro.setVisible(true); // Abre Tela Professor
    }//GEN-LAST:event_btnProfessorActionPerformed

    private void btnUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUsuarioActionPerformed
        this.dispose();  // Fecha Tela Curso
        TelaUsuario usu = new TelaUsuario(null, true);
        usu.carregarTabela("");
        usu.setVisible(true); //Abre Tela Usuario
    }//GEN-LAST:event_btnUsuarioActionPerformed

    private void btnDisciplinaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDisciplinaActionPerformed
        this.dispose(); // Fecha Tela Curso
        TelaDisciplina dis = new TelaDisciplina(null, true);
        dis.carregarTabela("");
        dis.setVisible(true); // Abre Tela Disciplina
    }//GEN-LAST:event_btnDisciplinaActionPerformed

    private void btnAlunoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlunoActionPerformed
        this.dispose(); // Fecha Tela Curso
        TelaAluno alu = new TelaAluno(null, true);
        alu.carregarTabela("");
        alu.setVisible(true); // Abre Tela Aluno
    }//GEN-LAST:event_btnAlunoActionPerformed

    private void btnBackupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackupActionPerformed
        this.dispose(); // Fecha Tela Curso
        TelaBackup bac = new TelaBackup(null, true);
        bac.setVisible(true); // Abre Tela Backup
    }//GEN-LAST:event_btnBackupActionPerformed

    private void btnVoltar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltar1ActionPerformed
        this.dispose(); // Fecha Tela Curso
    }//GEN-LAST:event_btnVoltar1ActionPerformed

    private void btnCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCadastrarActionPerformed
        this.enable();
        TelaCadCurso cadcur = new TelaCadCurso(null, true);
        cadcur.setCadastrar(true); //true para cadastrar
        cadcur.autoIncremento(); //setar o valor do código
        cadcur.setVisible(true);
        //Recarregar a tabela
        carregarTabela("");
    }//GEN-LAST:event_btnCadastrarActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        if(tblCurso.getSelectedRow() == -1){ // Não selecionou nenhuma Linha
            JOptionPane.showMessageDialog(null,"Selecione um curso para alterar", "Selecione o curso", JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {          
            //Obtem os dados de acordo com a linha ao qual foi selecionada.
            Integer linha = tblCurso.getSelectedRow();
            cur.setCodigo((Integer)tblCurso.getValueAt(linha, 0));
            cur.setNome((String)tblCurso.getValueAt(linha, 1));
            cur.setTipoCurso((String)tblCurso.getValueAt(linha, 2)); 
            cur.setAnoInicio((Integer)tblCurso.getValueAt(linha, 3));
            cur.setDuracao((Integer)tblCurso.getValueAt(linha, 4));
            cur.setCredito((Integer)tblCurso.getValueAt(linha, 5));
            cur.setTempoCredito((Integer)tblCurso.getValueAt(linha, 6));
            
            //Manda os dados para a tela de Alterar
            this.enable();
            TelaCadCurso altcur = new TelaCadCurso(null, true);
            altcur.setTitle("Alterar Curso");
            altcur.setCadastrar(false); //false para alterar
            altcur.recebeObjejo(cur);
            altcur.setVisible(true);
            //Recarregar a tabela
            carregarTabela(txtPesquisaCurso.getText().trim());
        } 
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
       if(tblCurso.getSelectedRow() == -1){ // Não selecionou nenhuma Linha
            JOptionPane.showMessageDialog(null,"Selecione um curso para excluir", 
                    "Selecione o curso", JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            Object[] opcoes = { "Sim", "Não" };
             //Obtem os dados de acordo com a linha ao qual foi selecionada.
            Integer linha = tblCurso.getSelectedRow();
            Integer resposta =JOptionPane.showOptionDialog(null,"Deseja excluir este curso ?", "Exclusão",JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null,
                    opcoes, opcoes[1]);
            
            // Verifica Resposta do Usuário
            if(resposta == JOptionPane.OK_OPTION){

                //excluir
                if(curDao.excluirCurso((Integer)tblCurso.getValueAt(linha, 0))){
                    JOptionPane.showMessageDialog(null,"Exclusão Realizada com Sucesso", 
                        "Excluir", JOptionPane.INFORMATION_MESSAGE);
                    //recarregar o jTable
                    carregarTabela("");
                }
                else{
                    JOptionPane.showMessageDialog(null,"Nao foi possível realizar a exclusão", 
                        "Excluir", JOptionPane.INFORMATION_MESSAGE);
                }
            }
            else{
                JOptionPane.showMessageDialog(null,"Exclusão cancelada", "Exclusão", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed
        carregarTabela(txtPesquisaCurso.getText().trim());
    }//GEN-LAST:event_btnPesquisarActionPerformed

    private void btnCarregartodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCarregartodosActionPerformed
        carregarTabela("");
    }//GEN-LAST:event_btnCarregartodosActionPerformed

    private void tblCursoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCursoMouseClicked
       if(evt.getClickCount() == 2){
            if(tblCurso.getSelectedRow() == -1){ // Não selecionou nenhuma Linha
                JOptionPane.showMessageDialog(null,"Selecione um curso para visualizar", "Selecione o curso", JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {    
                Integer linha = tblCurso.getSelectedRow();
                //seta o objeto curso de acordo com o que foi selecionado
                cur.setCodigo((Integer)tblCurso.getValueAt(linha, 0));
                cur.setNome((String)tblCurso.getValueAt(linha, 1));
                cur.setTipoCurso((String)tblCurso.getValueAt(linha, 2)); 
                cur.setAnoInicio((Integer)tblCurso.getValueAt(linha, 3));
                cur.setDuracao((Integer)tblCurso.getValueAt(linha, 4));
                cur.setCredito((Integer)tblCurso.getValueAt(linha, 5));
                cur.setTempoCredito((Integer)tblCurso.getValueAt(linha, 6));
                //Manda os dados para a tela de Alterar
                this.enable();
                TelaCadCurso visucur = new TelaCadCurso(null, true);
                visucur.visualizarObjeto(cur);
                visucur.setTitle("Visualizar Curso");
                visucur.setVisible(true);
        }
       }
    }//GEN-LAST:event_tblCursoMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose(); // Fecha Tela Curso
        TelaTurma tur = new TelaTurma(null, true);
        tur.carregarTabela("");
        tur.setVisible(true); // Abre Tela Turma
    }//GEN-LAST:event_jButton1ActionPerformed

    public void carregarTabela(String valor){
        List<CursoBD> listaCurso = new ArrayList<CursoBD>();
               
        if(cmbPesquisar.getSelectedIndex() == 0){          
            listaCurso = curDao.filtro("nome_curso", valor);
        }else{
            if(cmbPesquisar.getSelectedIndex() == 1){
                listaCurso = curDao.filtro("tipo_curso", valor);
            }else
            {
                listaCurso = curDao.filtro("ano_inic_curso", valor);
            }
        }
         
        DefaultTableModel tbm = (DefaultTableModel)tblCurso.getModel();    

        for(int i = tbm.getRowCount()-1; i >= 0; i--){ 
            tbm.removeRow(i); 
        } 
        int i = 0; 
        for(CursoBD cr : listaCurso){            
        tbm.addRow(new String[1]); 
            tblCurso.setValueAt(cr.getCodigo(), i, 0); //Objeto Linha Coluna
            tblCurso.setValueAt(cr.getNome(), i, 1); 
            tblCurso.setValueAt(cr.getTipoCurso(), i, 2);
            tblCurso.setValueAt(cr.getAnoInicio(), i, 3); 
            tblCurso.setValueAt(cr.getDuracao(), i, 4);
            tblCurso.setValueAt(cr.getCredito(), i, 5);
            tblCurso.setValueAt(cr.getTempoCredito(), i, 6);
            i++;
        }
         centralizarTable();
    }

    public void centralizarTable(){
        DefaultTableCellRenderer centro = new DefaultTableCellRenderer();
        centro.setHorizontalAlignment(SwingConstants.CENTER ); 
        tblCurso.getColumnModel().getColumn(0).setCellRenderer(centro);
        tblCurso.getColumnModel().getColumn(2).setCellRenderer(centro);     
        tblCurso.getColumnModel().getColumn(3).setCellRenderer(centro);     
        tblCurso.getColumnModel().getColumn(4).setCellRenderer(centro);
        tblCurso.getColumnModel().getColumn(5).setCellRenderer(centro);
        tblCurso.getColumnModel().getColumn(6).setCellRenderer(centro);
    }
    
    public static void main(String args[]) {
        
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaCurso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaCurso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaCurso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaCurso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TelaCurso dialog = new TelaCurso(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnAluno;
    private javax.swing.JButton btnBackup;
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JButton btnCarregartodos;
    private javax.swing.JButton btnCurso;
    private javax.swing.JButton btnDisciplina;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JButton btnProfessor;
    private javax.swing.JButton btnUsuario;
    private javax.swing.JButton btnVoltar1;
    private javax.swing.JComboBox cmbPesquisar;
    private javax.swing.ButtonGroup grupoCurso;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnlControle;
    private javax.swing.JTable tblCurso;
    private javax.swing.JTextField txtPesquisaCurso;
    // End of variables declaration//GEN-END:variables
}
